﻿package com.dd.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dd.dao.DdOrderGoodsDAO;
import com.dd.dao.DdShopgoodsDAO;
import com.dd.dao.DdUserDAO;
import com.dd.dao.DdUserpayDAO;
import com.dd.entity.DdOrder;
import com.dd.entity.DdOrderGoods;
import com.dd.entity.DdShop;
import com.dd.entity.DdShopgoods;
import com.dd.entity.DdSms;
import com.dd.entity.DdUser;
import com.dd.entity.DdUserorder;
import com.dd.entity.DdUserorder1;
import com.dd.entity.DdUserpay;
import com.dd.util.DateStrFormat;
import com.dd.util.PageList;

public class OrderAction extends BaseAction {

	/**
	 * 新建订单---(实物类)
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward neworder(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// HttpSession session = request.getSession();
		PrintWriter out = null;
		Integer logid = (Integer) request.getSession().getAttribute("logid");// 网站用户是否已经登录

		try {
			out = response.getWriter();
			if (logid == null) {// 网站用户尚未登录
				// 返回-1
				out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
				return null;
			}
			Date now = new Date();
			int hour = now.getHours();
			if (hour < 9 || hour > 21) {
				out
				.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-10</error></root>");
				return null;
			}
			DdOrderGoodsDAO ddOrderGoodsDAO = this.getDdOrderGoodsDAO();
			DdShopgoodsDAO ddShopgoodsDAO = this.getDdGoodDAO();
			String shopid = request.getParameter("shopid");// 商家ID
			String goodlist = request.getParameter("goodlist");// 订单内容--改id
			String address = request.getParameter("address");// 送货地址
			String senddate = request.getParameter("senddate");// 送货时间
			String phone = request.getParameter("phone");// 联系电话
			String name = request.getParameter("name");// 姓名
			String sex = request.getParameter("sex");// 性别
			String phone2 = request.getParameter("phone2");// 订货人电话
			String name2 = request.getParameter("name2");// 订货人姓名
			String sex2 = request.getParameter("sex2");// 订货人性别
			String cardtype = request.getParameter("cardtype");// 卡片类型
			String cardvalue = request.getParameter("cardvalue");// 卡片留言
			String signtype = request.getParameter("signtype");// 是否签名
			String signvalue = request.getParameter("signvalue");// 签名
			String remark = request.getParameter("remark");// 备注
			String selectsize = request.getParameter("selectsize"); //蛋糕尺寸  --非蛋糕类无此属性
			if (!this.ischeck(shopid, goodlist, address, name, sex, phone)) {// 信息填写不完整
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-2</error></root>");
				return null;
			}
			int goodid = Integer.parseInt(goodlist);// 礼物只传ID
			DdShopgoods ddShopgoods = ddShopgoodsDAO.findById(goodid);
			if (this.getShopService().getDdShopDAO().findById(
					Integer.parseInt(shopid)) == null) {
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-3</error></root>");
				return null;
			}
			// order子表(详情)
			DdUserorder duo = this.setDdUserorder(shopid, goodlist, address,
					name, sex, phone, logid + "", remark);// 获取订单（订餐）对象

			// order主表(简要信息)
			DdOrder ddorder = this.setDdOrder(duo);
			this.getDdOrderDAO().save(ddorder);
			duo.setOrderid(ddorder.getId());
			duo.setPhone2(phone2);
			duo.setName2(name2);
			duo.setSex2(Integer.parseInt(sex2));
			duo.setCardtype(cardtype);
			duo.setCardvalue(cardvalue);
			duo.setSenddate(senddate);
			duo.setSigntype(signtype);
			duo.setSignvalue(signvalue);
			duo.setSize((selectsize==null?"":selectsize));
			this.getDdUserorderDAO().save(duo);// 添加

			float sumprice = 0;

			DdOrderGoods ddOrderGoods = new DdOrderGoods();
			ddOrderGoods.setNum(1);
			ddOrderGoods.setOrderid(ddorder.getId());
			ddOrderGoods.setPrice(ddShopgoods.getPrice());
			ddOrderGoods.setShopid(duo.getShopid());
			ddOrderGoods.setShopgoodsid(ddShopgoods.getId());
			ddOrderGoodsDAO.save(ddOrderGoods);
			if(ddShopgoods.getStyleType().intValue()==138){
				int size = Integer.parseInt(selectsize);
				if (size == 6) {
					sumprice = ddShopgoods.getDiscount6();
				} else if (size == 8) {
					sumprice = ddShopgoods.getDiscount8();
				} else if (size == 10) {
					sumprice = ddShopgoods.getDiscount10();
				} else {
					sumprice = ddShopgoods.getDiscount12();
				}
			}else{
				DdShop ds = this.getDdShopDAO().findById(ddShopgoods.getShopid());
				sumprice = this.getShopService().getPrice2(ds, ddShopgoods, senddate);
			}
			
			ddorder.setPrice(sumprice);
			this.getDdOrderDAO().update(ddorder);
			out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>"
							+ (ddorder.getId() + 100000000) + "</result></root>");
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}

	}
	//获取餐厅预订价
	public float getPrice(DdShop ds, String date,int num,String ltype) {
		try {
			return ds.getBookprice();
//			if(ds.getType().intValue()==2){
//				return ds.getBookprice();
//			}
//			int ordnum = 1;
//			if (ds.getBooktype().intValue() == 1) {
//				ordnum = num;
//			}
//			if (ds.getDiscountbeg() == null || ds.getDiscountend() == null) {
//				if(ltype.equals("1")){
//					return ds.getBookprice()*ordnum;
//				}else{
//					return ds.getBookprice2()*ordnum;
//				}
//				
//			}
//			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//			Date s_date = (Date) sdf.parse(date);
//			if (s_date.getTime() > ds.getDiscountbeg().getTime()
//					&& s_date.getTime() < ds.getDiscountend().getTime()) {
//				if(ltype.equals("1")){
//					return ds.getDiscountprice();
//				}else{
//					return ds.getDiscountprice2();
//				}
//				
//			}
//			if(ltype.equals("1")){
//				return ds.getBookprice()*ordnum;
//			}else{
//				return ds.getBookprice2()*ordnum;
//			}
		} catch (Exception e) {
			// e.printStackTrace();
			return ds.getBookprice();
		}
	}
	
	//获取运动场所价格
	public float getPrice1(DdShop ds, String date,int from ,int to) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date s_date = (Date) sdf.parse(date);
			int day = s_date.getDay();//获取星期几  0-星期日.1-6=星期1-6	
			List list = this.getDdGoodDAO().findByShopidAndDay(ds.getId(),day);
			DdShopgoods dsd1 = (DdShopgoods)list.get(0);
			DdShopgoods dsd2 = (DdShopgoods)list.get(1);
			float price = 0F;
			for(int i=from;i<to;i++){
				if(i<ds.getStyleType().intValue()){//早上价格
					price = price+dsd1.getPrice();
				}else{//晚上价格
					price = price+dsd2.getPrice();
				}
			}
			
			return price;
		} catch (Exception e) {
			 e.printStackTrace();
			return 0F;
		}
	}
	public boolean getIsRejectt(DdShop ds, String date, String loge) {
		boolean flag = false;
		try {
			date = date.substring(0, 13);
			int loge_i = 0;
			if (loge.equals("只订包厢")) {
				loge_i = 1;
			}
			flag = this.getDdShoprejectDAO().checkReject(ds.getId(), date,
					loge_i);
			return flag;
		} catch (Exception e) {
			// e.printStackTrace();
			return flag;
		}
	}
	public boolean getIsRejectt2(DdShop ds, String date, String loge) {
		boolean flag = false;
		try {
			date = date.substring(0, 13);
			int loge_i = 0;
			if (loge.equals("只订包厢")) {
				loge_i = 1;
			}
			flag = this.getDdShoprejectDAO().checkReject(ds.getId(), date,
					loge_i);
			return flag;
		} catch (Exception e) {
			// e.printStackTrace();
			return flag;
		}
	}
	/**
	 * 新建订单---(订位)
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward neworder1(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// HttpSession session = request.getSession();
		PrintWriter out = null;
		Integer logid = (Integer) request.getSession().getAttribute("logid");// 网站用户是否已经登录
		
		try {
			out = response.getWriter();
			if (logid == null) {// 网站用户尚未登录
				// 返回-1
				out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
				return null;
			}
			long lastorder = 0L;
			if(request.getSession().getAttribute("lastorder")!=null){
				lastorder = (Long) request.getSession().getAttribute("lastorder");//
			}
			if((System.currentTimeMillis()-lastorder)<60000){
				out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-6</error></root>");
				return null;
			}
			request.getSession().setAttribute("lastorder",System.currentTimeMillis());
			Date now = new Date();
			int hour = now.getHours();
			boolean nokefu=false;
			if (hour < 9 || hour >= 21) {
				nokefu=true;
//				out
//				.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-10</error></root>");
//				return null;
			}
			DdUser du = this.getDdUserDAO().findById(logid);
//			if((System.currentTimeMillis()-du.getLastorder())<1200000){
//				out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-6</error></root>");
//				return null;
//			}
			String shopid = request.getParameter("shopid");// 商家ID
			String phone = request.getParameter("phone");// 联系电话
			String name = request.getParameter("name");// 姓名
			String sex = request.getParameter("sex");// 性别
			String people = request.getParameter("people");// 人数
			String loge = request.getParameter("loge");// 包厢位置
			String logetype = request.getParameter("logetype");// 包厢位置
			String orderdate1 = request.getParameter("orderdate1");// 预计达到时间
			String remark = request.getParameter("remark");// 备注
			if (!this.ischeck1(shopid, name, sex, phone, people, loge,
					orderdate1)) {// 信息填写不完整
				out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-2</error></root>");
				return null;
			}
			
			DdShop ds = this.getDdShopDAO().findById(Integer.parseInt(shopid));
			
			if (ds == null||ds.getAlive().intValue()==0) {
				out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-3</error></root>");
				return null;
			}
			ds.setHot(ds.getHot()+(1+new Random().nextInt(10)));
			this.getDdShopDAO().saveOrUpdate(ds);
			boolean b = true;
			du.setLastorder(System.currentTimeMillis());
			this.getDdUserDAO().saveOrUpdate(du);
			if(ds.getBookprice().floatValue()==0F||b){
				//不要定金返回-4
				DdUserorder1 duo = this.setDdUserorder1(shopid, name, sex, phone,
						logid + "", people, loge, orderdate1, remark);// 获取订单（订位）对象
				DdOrder ddorder = this.setDdOrder(duo);
				// 2011-11-22
				int code = new Random().nextInt(800000)+100000;
				ddorder.setCode(code);
				ddorder.setStatus(1);
				ddorder.setPaydate(new Date());
				String msg = "【蛋订网】您好，您有一条新订单，请尽快登陆后台确认/取消订单";
				ddorder.setPrice(ds.getBookprice());
				if (ddorder.getPrice().floatValue() != ds.getBookprice()
						.floatValue()) {
					ddorder.setRemark(ds.getDiscountremark());
				}
				
				this.getDdOrderDAO().save(ddorder);
				duo.setOrderid(ddorder.getId());
				this.getDdUserorder1DAO().save(duo);// 添加	
				DdSms dsms = new DdSms();
				dsms.setSendmsg(msg);
				dsms.setOrderid(ddorder.getId());
				dsms.setOrderstatus(1);
				dsms.setTypes(1);
				dsms.setStatus(0);
				dsms.setPhone(ds.getMobile());
				this.getDdSmsDAO().saveOrUpdate(dsms);
				if(nokefu){
					//没客服了,前段提示要改
					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>-5</result></root>");
				}else{
					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>-4</result></root>");
				}
				
				return null;
			}
			if(ds.getType().intValue()==1){//餐厅
				if(ds.getAlive().intValue()==0){
					out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-5</error></root>");
					return null;
				}
				if (this.getIsRejectt(ds, orderdate1, loge)) {
					// 满座
					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>3</result></root>");
					return null;
				}
				DdUserorder1 duo = this.setDdUserorder1(shopid, name, sex, phone,
						logid + "", people, loge, orderdate1, remark);// 获取订单（订位）对象

				DdOrder ddorder = this.setDdOrder(duo);
				// 2011-11-22
				ddorder.setPrice(this.getPrice(ds, orderdate1.substring(0, 10),Integer.parseInt(people),logetype));
				if (ddorder.getPrice().floatValue() != ds.getBookprice()
						.floatValue()) {
					ddorder.setRemark(ds.getDiscountremark());
				}
				this.getDdOrderDAO().save(ddorder);
				duo.setOrderid(ddorder.getId());
				this.getDdUserorder1DAO().save(duo);// 添加
				if ((du.getAmount() + du.getAmountfree()) >= ddorder.getPrice()) {
					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>"
									+ (ddorder.getId() + 100000000)
									+ "</result></root>");

				} else {
					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>"
									+ (-1 * (ddorder.getId() + 100000000))
									+ "</result></root>");
				}
			}
//			else if(ds.getType().intValue()==5){ //运动场所
//				String hourfrom = request.getParameter("hourfrom");
//				String hourto = request.getParameter("hourto");
//				DdUserorder1 duo = this.setDdUserorder1_sport(shopid, name, sex, phone,
//						logid + "", people, loge, orderdate1, remark,hourfrom,hourto);// 获取订单（订位）对象
//				DdOrder ddorder = this.setDdOrder(duo);
//				// 2011-11-22
//				ddorder.setPrice(this.getPrice1(ds, orderdate1,Integer.parseInt(hourfrom),Integer.parseInt(hourto)));
//				if (ddorder.getPrice().floatValue() != ds.getBookprice()
//						.floatValue()) {
//					ddorder.setRemark(ds.getDiscountremark());
//				}
//				this.getDdOrderDAO().save(ddorder);
//				duo.setOrderid(ddorder.getId());
//				this.getDdUserorder1DAO().save(duo);// 添加
//				if ((du.getAmount() + du.getAmountfree()) >= ddorder.getPrice()) {
//					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>"
//									+ (ddorder.getId() + 100000000)
//									+ "</result></root>");
//
//				} else {
//					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>"
//									+ (-1 * (ddorder.getId() + 100000000))
//									+ "</result></root>");
//				}
//				
//				
//			}else if(ds.getType().intValue()==2){ //娱乐场所
//				if (this.getIsRejectt2(ds, orderdate1, loge)) {
//					// 满座
//					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>3</result></root>");
//					return null;
//				}
//				
//				if (hour < 15 ) {
//					// 太早预定
//					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>4</result></root>");
//					return null;
//				}
//				DdUserorder1 duo = this.setDdUserorder1(shopid, name, sex, phone,
//						logid + "", people, loge, orderdate1, remark);// 获取订单（订位）对象
//
//				DdOrder ddorder = this.setDdOrder(duo);
//				// 2011-11-22
//				ddorder.setPrice(ds.getBookprice());
//				this.getDdOrderDAO().save(ddorder);
//				duo.setOrderid(ddorder.getId());
//				this.getDdUserorder1DAO().save(duo);// 添加
//				if ((du.getAmount() + du.getAmountfree()) >= ddorder.getPrice()) {
//					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>"
//									+ (ddorder.getId() + 100000000)
//									+ "</result></root>");
//
//				} else {
//					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>"
//									+ (-1 * (ddorder.getId() + 100000000))
//									+ "</result></root>");
//				}
//			}
			
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 新建订单---(订位,订菜)
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward neworder2(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// HttpSession session = request.getSession();
		PrintWriter out = null;
		Integer logid = (Integer) request.getSession().getAttribute("logid");// 网站用户是否已经登录
		try {
			out = response.getWriter();
			if (logid == null) {// 网站用户尚未登录
				// 返回-1
				out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
				return null;
			}
			Date now = new Date();
			int hour = now.getHours();
			if (hour < 9 || hour > 21) {
				out
				.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-10</error></root>");
				return null;
			}
			DdUser du = this.getDdUserDAO().findById(logid);
			String shopid = request.getParameter("shopid");// 商家ID
			String phone = request.getParameter("phone");// 联系电话
			String name = request.getParameter("name");// 姓名
			String sex = request.getParameter("sex");// 性别
			String people = request.getParameter("people");// 人数
			String loge = request.getParameter("loge");// 包厢位置
			String logetype = request.getParameter("logetype");// 包厢位置
			String orderdate1 = request.getParameter("orderdate1");// 预计达到时间
			String remark = request.getParameter("remark");// 备注
			if (!this.ischeck1(shopid, name, sex, phone, people, loge,
					orderdate1)) {// 信息填写不完整
				out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-2</error></root>");
				return null;
			}
			DdShop ds = this.getDdShopDAO().findById(Integer.parseInt(shopid));
			
			if (ds == null) {
				out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-3</error></root>");
				return null;
			}
			if(ds.getType().intValue()==1){//餐厅
				if(ds.getAlive().intValue()==0){
					out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-5</error></root>");
					return null;
				}
				if (this.getIsRejectt(ds, orderdate1, loge)) {
					// 满座
					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>3</result></root>");
					return null;
				}
				DdUserorder1 duo = this.setDdUserorder1(shopid, name, sex, phone,
						logid + "", people, loge, orderdate1, remark);// 获取订单（订位）对象

				DdOrder ddorder = this.setDdOrder(duo);
				// 2011-11-22
				ddorder.setPrice(this.getPrice(ds, orderdate1.substring(0, 10),Integer.parseInt(people),logetype));
				if (ddorder.getPrice().floatValue() != ds.getBookprice()
						.floatValue()) {
					ddorder.setRemark(ds.getDiscountremark());
				}
				this.getDdOrderDAO().save(ddorder);
				duo.setOrderid(ddorder.getId());
				this.getDdUserorder1DAO().save(duo);// 添加
				if ((du.getAmount() + du.getAmountfree()) >= ddorder.getPrice()) {
					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>"
									+ (ddorder.getId() + 100000000)
									+ "</result></root>");

				} else {
					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>"
									+ (-1 * (ddorder.getId() + 100000000))
									+ "</result></root>");
				}
			}else if(ds.getType().intValue()==5){ //运动场所
				String hourfrom = request.getParameter("hourfrom");
				String hourto = request.getParameter("hourto");
				DdUserorder1 duo = this.setDdUserorder1_sport(shopid, name, sex, phone,
						logid + "", people, loge, orderdate1, remark,hourfrom,hourto);// 获取订单（订位）对象
				DdOrder ddorder = this.setDdOrder(duo);
				// 2011-11-22
				ddorder.setPrice(this.getPrice1(ds, orderdate1,Integer.parseInt(hourfrom),Integer.parseInt(hourto)));
				if (ddorder.getPrice().floatValue() != ds.getBookprice()
						.floatValue()) {
					ddorder.setRemark(ds.getDiscountremark());
				}
				this.getDdOrderDAO().save(ddorder);
				duo.setOrderid(ddorder.getId());
				this.getDdUserorder1DAO().save(duo);// 添加
				if ((du.getAmount() + du.getAmountfree()) >= ddorder.getPrice()) {
					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>"
									+ (ddorder.getId() + 100000000)
									+ "</result></root>");

				} else {
					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>"
									+ (-1 * (ddorder.getId() + 100000000))
									+ "</result></root>");
				}
				
				
			}else if(ds.getType().intValue()==2){ //娱乐场所
				if (this.getIsRejectt2(ds, orderdate1, loge)) {
					// 满座
					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>3</result></root>");
					return null;
				}
				
				if (hour < 15 ) {
					// 太早预定
					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>4</result></root>");
					return null;
				}
				DdUserorder1 duo = this.setDdUserorder1(shopid, name, sex, phone,
						logid + "", people, loge, orderdate1, remark);// 获取订单（订位）对象

				DdOrder ddorder = this.setDdOrder(duo);
				// 2011-11-22
				ddorder.setPrice(ds.getBookprice());
				this.getDdOrderDAO().save(ddorder);
				duo.setOrderid(ddorder.getId());
				this.getDdUserorder1DAO().save(duo);// 添加
				if ((du.getAmount() + du.getAmountfree()) >= ddorder.getPrice()) {
					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>"
									+ (ddorder.getId() + 100000000)
									+ "</result></root>");

				} else {
					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>"
									+ (-1 * (ddorder.getId() + 100000000))
									+ "</result></root>");
				}
			}
			
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}
	
	public ActionForward payfororder(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		Integer logid = (Integer) request.getSession().getAttribute("logid");// 网站用户是否已经登录
		if (logid == null) {// 网站用户尚未登录
			// 返回-1
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
			return null;
		}
		try {
			DdUser du = this.getDdUserDAO().findById(logid);
			String orderid = request.getParameter("orderid");// 商家ID
			if (orderid == null || orderid.equals("")) {// 信息填写不完整
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-2</error></root>");
				return null;
			}
			int oid = Integer.parseInt(orderid);
			if (oid > 100000000) {
				oid = oid - 100000000;
			}
			int code = new Random().nextInt(800000)+100000;
			DdOrder ddorder = this.getDdOrderDAO().findById(oid);
			ddorder.setCode(code);
			// 状态为0才能支付
			if (ddorder == null || ddorder.getStatus().intValue() != 0) {
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-2</error></root>");
				return null;
			}
			if ((du.getAmountfree() + du.getAmount()) < ddorder.getPrice()) {
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>2</result></root>");
				return null;
			} else {
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>1</result></root>");
				// 付款,真实币与虚拟币分开计算
				// 退款时候同样分开退
				DdUserpay up = new DdUserpay();
				DdSms dsms = new DdSms();
				up.setUserid(du.getId());
				up.setOrderid(ddorder.getId());
				up.setPaydate(new Date());
				up.setPaytype(2);
				String msg = "";
				DdShop ds = this.getDdShopDAO().findById(ddorder.getShopid());
				if (du.getAmount() >= ddorder.getPrice()) {
					du.setAmount(du.getAmount() - ddorder.getPrice());
					up.setAmount(ddorder.getPrice());
				} else {
					up.setAmount(du.getAmount());
					du.setAmountfree(du.getAmountfree() + du.getAmount()
							- ddorder.getPrice());
					up.setAmountfree(ddorder.getPrice() - du.getAmount());
					du.setAmount(0F);
				}
				up.setStatus(2);
				up.setShopid(ddorder.getShopid());
				if (ddorder.getType() == 1) {// 订餐、订实物
					DdUserorder duo = (DdUserorder) this.getDdUserorderDAO()
							.findByOrderid(ddorder.getId()).get(0);
					up.setRemark("订单号:" + (ddorder.getId() + 100000000));
					msg = "【蛋订网】您好，您有一条新订单，请尽快登陆后台确认/取消订单";
				} else if (ddorder.getType() == 2) {// 订位置
					up.setRemark("订单号:" + (ddorder.getId() + 100000000));
					msg = "【蛋订网】您好，您有一条新订单，请尽快登陆后台确认/取消订单";
				}

				this.getDdUserpayDAO().saveOrUpdate(up);
				this.getDdUserDAO().saveOrUpdate(du);
				ddorder.setStatus(1);
				ddorder.setPaydate(new Date());
				this.getDdOrderDAO().save(ddorder);
				// todo 用户付款,添加发送短信任务
				dsms.setSendmsg(msg);
				dsms.setOrderid(ddorder.getId());
				dsms.setOrderstatus(1);
				dsms.setTypes(1);
				dsms.setStatus(0);
				dsms.setPhone(ds.getMobile());
				this.getDdSmsDAO().saveOrUpdate(dsms);
				return null;
			}
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward confirmorder(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		Integer logid = (Integer) request.getSession().getAttribute("logid");// 网站用户是否已经登录
		if (logid == null) {// 网站用户尚未登录
			// 返回-1
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
			return null;
		}
		try {
			DdUser du = this.getDdUserDAO().findById(logid);
			String orderid = request.getParameter("orderid");// 商家ID
			if (orderid == null || orderid.equals("")) {// 信息填写不完整
				out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-2</error></root>");
				return null;
			}
			DdOrder ddorder = this.getDdOrderDAO().findById(
					Integer.parseInt(orderid));
			// 状态为2的才能确认
			if (ddorder == null || ddorder.getStatus().intValue() != 2) {
				out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>2</error></root>");
				return null;
			}
			DdShop ds = this.getDdShopDAO().findById(ddorder.getShopid());
			try {
				if(ds.getType().intValue()==1||ds.getType().intValue()==2||ds.getType().intValue()==3){
					int code = Integer.parseInt(request.getParameter("code"));
					if (code != ddorder.getCode()) {
						out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-3</error></root>");
						return null;
					}
				}
				
			} catch (Exception ee) {
				out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-3</error></root>");
				return null;
			}
			ddorder.setStatus(3);
			ddorder.setConfirmdate(new Date());
			ddorder.setScore(Integer.parseInt(request.getParameter("score")));
			ddorder.setRemark(request.getParameter("remark"));
			ds.setScore(ds.getScore()+ddorder.getScore());
			this.getDdShopDAO().saveOrUpdate(ds);
			this.getDdOrderDAO().save(ddorder);
			du.setScore(du.getScore()+ds.getAddscore());
			this.getDdUserDAO().saveOrUpdate(du);
			out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>1</result></root>");

			return null;

		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward cancelorder(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		Integer logid = (Integer) request.getSession().getAttribute("logid");// 网站用户是否已经登录
		if (logid == null) {// 网站用户尚未登录
			// 返回-1
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
			return null;
		}
		try {
			DdUser du = this.getDdUserDAO().findById(logid);
			String orderid = request.getParameter("orderid");// 商家ID
			if (orderid == null || orderid.equals("")) {// 信息填写不完整
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-2</error></root>");
				return null;
			}
			DdOrder ddorder = this.getDdOrderDAO().findById(
					Integer.parseInt(orderid));
			if (ddorder == null || ddorder.getStatus().intValue() != 0) {
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-2</error></root>");
				return null;
			}
			ddorder.setStatus(-1);
			this.getDdOrderDAO().save(ddorder);
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>1</result></root>");

			return null;

		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	private DdOrder setDdOrder(DdUserorder ddUserorder) {
		DdOrder ddOrder = new DdOrder();
		ddOrder.setShopid(ddUserorder.getShopid());
		ddOrder.setStatus(ddUserorder.getStatus());
		ddOrder.setType(1);
		ddOrder.setUserid(ddUserorder.getUserid());
		return ddOrder;
	}

	private DdOrder setDdOrder(DdUserorder1 ddUserorder) {
		DdOrder ddOrder = new DdOrder();
		ddOrder.setShopid(ddUserorder.getShopid());
		ddOrder.setStatus(ddUserorder.getStatus());
		ddOrder.setType(2);
		ddOrder.setUserid(ddUserorder.getUserid());
		return ddOrder;
	}

	/**
	 * 修改订单状态
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward updateorder(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		Integer logid = (Integer) request.getSession().getAttribute("logid");// 网站用户是否已经登录
		if (logid == null) {// 网站用户尚未登录
			// 返回-1
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
			return null;
		}

		try {
			String id = request.getParameter("id");// 订单ID
			// 订单状态 -3 系统取消,-2 商家取消 ,-1用户取消 0待付款 1已付款 2商家确认 3用户确认消费
			int status = Integer.parseInt(request.getParameter("status"));
			if (id != null && id.trim().length() > 0) {
				DdOrder ddorder = this.getDdOrderDAO().findById(
						Integer.parseInt(id));
				if (ddorder == null) {// 订单不存在
					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>2</result></root>");
					return null;
				}

				if (status == -1) { // 用户取消--只有0的才能取消
					if (ddorder.getStatus().intValue() != 0) {
						out
								.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>0</result></root>");
						return null;
					}
					ddorder.setStatus(status);
					this.getDdOrderDAO().save(ddorder);
				} else if (status == -2) { // 商户取消只能在另外个action里面取消
					out
							.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>0</result></root>");
					return null;
				} else if (status == 2) { // 商家确认同理
					out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>0</result></root>");
					return null;

				} else if (status == 3) {
					ddorder.setStatus(status);
					this.getDdOrderDAO().save(ddorder);
				}
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>1</result></root>");
			} else {
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>0</result></root>");

			}
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	private DdUserorder setDdUserorder(String shopid, String goodslist,
			String address, String name, String sex, String phone,
			String userid, String remark) {
		DdUserorder duo = new DdUserorder();
		duo.setGoodslist(goodslist);
		duo.setShopid(Integer.parseInt(shopid));
		duo.setSex(Integer.parseInt(sex));
		duo.setAddress(address);
		duo.setName(name);
		duo.setPhone(phone);
		duo.setStatus(0);
		duo.setType(1);
		duo.setUserid(Integer.parseInt(userid));
		duo.setRemark(remark);
		return duo;
	}

	private DdUserorder1 setDdUserorder1(String shopid, String name,
			String sex, String phone, String userid, String people,
			String loge, String orderdate1, String remark) {
		try {
			DdUserorder1 duo = new DdUserorder1();
			duo.setShopid(Integer.parseInt(shopid));
			duo.setSex(Integer.parseInt(sex));
			duo.setName(name);
			duo.setPhone(phone);
			duo.setPeople(people);
			duo.setLoge(loge);
			//System.out.println(orderdate1);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			Date s_date = (Date) sdf.parse(orderdate1);
			Date d = DateStrFormat.getNewDate(orderdate1 + ":00");
			duo.setOrderdate1(s_date);
			duo.setStatus(0);
			duo.setType(2);
			duo.setUserid(Integer.parseInt(userid));
			duo.setRemark(remark);
			return duo;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	private DdUserorder1 setDdUserorder1_sport(String shopid, String name,
			String sex, String phone, String userid, String people,
			String loge, String orderdate1, String remark,String from,String to) {
		try {
			DdUserorder1 duo = new DdUserorder1();
			duo.setShopid(Integer.parseInt(shopid));
			duo.setSex(Integer.parseInt(sex));
			duo.setName(name);
			duo.setPhone(phone);
			duo.setPeople(people);
			duo.setLoge(loge);
			//System.out.println(orderdate1);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date s_date = (Date) sdf.parse(orderdate1);
			duo.setOrderdate1(s_date);
			duo.setStatus(0);
			duo.setType(2);
			duo.setUserid(Integer.parseInt(userid));
			duo.setRemark(remark);
			duo.setFromto(from+"点到"+to+"点");
			return duo;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 验证.新订单---(送餐)
	 * 
	 * @param shopid
	 *            商家ID
	 * @param goodlist
	 *            商品信息
	 * @param address
	 *            送餐地址
	 * @param name
	 *            客户姓名
	 * @param sex
	 *            性别
	 * @param phone
	 *            联系电话
	 * @return
	 */
	private boolean ischeck(String shopid, String goodlist, String address,
			String name, String sex, String phone) {
		if (shopid == null || shopid.trim().length() == 0) {
			return false;
		}
		if (goodlist == null || goodlist.trim().length() == 0) {
			return false;
		}
		if (address == null || address.trim().length() == 0) {
			return false;
		}
		if (name == null || name.trim().length() == 0) {
			return false;
		}
		if (sex == null || sex.trim().length() == 0) {
			return false;
		}
		if (phone == null || phone.trim().length() == 0) {
			return false;
		}
		return true;
	}

	/**
	 * 订位 验证.新订单---(送餐)
	 * 
	 * @param shopid
	 *            商家ID
	 * @param goodlist
	 *            商品信息
	 * @param address
	 *            送餐地址
	 * @param name
	 *            客户姓名
	 * @param sex
	 *            性别
	 * @param phone
	 *            联系电话
	 * @return
	 */
	private boolean ischeck1(String shopid, String name, String sex,
			String phone, String people, String loge, String orderdate1) {
		if (shopid == null || shopid.trim().length() == 0) {
			return false;
		}

		if (name == null || name.trim().length() == 0) {
			return false;
		}
		if (sex == null || sex.trim().length() == 0) {
			return false;
		}
		if (phone == null || phone.trim().length() == 0) {
			return false;
		}
		try{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			Date s_date = (Date) sdf.parse(orderdate1);
		}catch(Exception e){
			return false;
		}
		return true;
	}

	/**
	 * 手机号验证
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward isphone(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response) {
		return null;
	}

	/**
	 * 查询订单
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward queryorder(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// HttpSession session = request.getSession();
		PrintWriter out = null;
		try {
			out = response.getWriter();
			Integer logid = (Integer) request.getSession()
					.getAttribute("logid");// 网站用户是否已经登录
			StringBuffer sbf = new StringBuffer(
					"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
			if (logid == null) {// 网站用户尚未登录
				// 返回-1
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
				return null;
			}

			String startdate = request.getParameter("startdate");// 订单创建的开始时间
			String enddate = request.getParameter("enddate");// 订单创建的结束时间
			String pageNo = request.getParameter("pageno");// 第几页
			String pageSize = request.getParameter("pagesize");// 每页记录数
			String status = request.getParameter("status");// 订单状态
			String shopname = request.getParameter("shopname");// 商家名称
			String orderid = request.getParameter("orderid");// 订单号/商家 --混合属性
			// --先判断是不是数字,如果是数字就当订单号,否则当商家名
			StringBuffer sqlwhere = new StringBuffer();

			if (orderid != null && orderid.trim().length() > 0) {
				try {
					int oid = Integer.parseInt(orderid) - 100000000;
					sqlwhere.append(" and a.id=" + oid + "");
				} catch (Exception ee) {
					shopname = orderid;
				}
			}

			if (startdate != null && startdate.trim().length() > 0) {
				sqlwhere.append(" and a.orderdate>='" + startdate + "'");
			}
			if (enddate != null && enddate.trim().length() > 0) {
				sqlwhere.append(" and a.orderdate<='" + enddate + "'");
			}

			if (status != null && status.trim().length() > 0) {
				sqlwhere.append(" and a.status in (" + status + ")");
			}
			if (shopname != null && shopname.trim().length() > 0) {
				sqlwhere.append(" and b.name like'%" + shopname + "%'");
			}
			sqlwhere.append(" and a.userid=" + logid);
			int i_pageNo = 1;
			if (pageNo != null && pageNo.trim().length() > 0) {
				i_pageNo = Integer.parseInt(pageNo);
			} else {
				i_pageNo = 1;
			}
			int i_pageSize = 10;
			if (pageSize != null && pageSize.trim().length() > 0) {
				i_pageSize = Integer.parseInt(pageSize);
			} else {
				i_pageSize = 10;
			}
			PageList list = this.getDdOrderDAO().findAll(sqlwhere.toString(),
					i_pageNo, i_pageSize);
			sbf.append("<ordershops total=\"" + list.getTotalCount() + "\">");
			for (int i = 0; i < list.size(); i++) {
				Object[] obj = (Object[]) list.get(i);
				DdOrder du = (DdOrder) obj[0];
				DdShop ds = (DdShop) obj[1];
				String date2 = "";
				String goodname = "";
				String phone = "0";
				if (du.getType().intValue() == 2) {
					// 定位
					DdUserorder1 duo = (DdUserorder1) this.getDdUserorder1DAO()
							.findByOrderid(du.getId()).get(0);
					date2 = DateStrFormat.getCurrentTime(duo.getOrderdate());
					phone = duo.getPhone();
					sbf.append("<ordershop shopid=\"" + ds.getId()
							+ "\" shopname=\"" + ds.getName() + "\" goodname=\""
							+ goodname + "\" id=\"" + du.getId() + "\"  orderid=\""
							+ (du.getId() + 100000000) + "\" price=\""
							+ du.getPrice() + "\" orderdate=\""
							+ DateStrFormat.getCurrentTime(du.getOrderdate())
							+ " \"  orderstatus=\"" + du.getStatus()
							+ " \" type=\"" + ds.getType() + "\" phone=\"" + phone
							+ "\" />");
				} else {
					
//					DdUserorder duo = (DdUserorder) this.getDdUserorderDAO()
//							.findByOrderid(du.getId()).get(0);
//					date2 = duo.getSenddate();
//					DdShopgoods dsg = this.getDdGoodDAO().findById(
//							Integer.parseInt(duo.getGoodslist()));
//					goodname = dsg.getName();
//					phone = duo.getPhone2();
				}
				
			}
			sbf.append("</ordershops></root>");
			// System.out.print(sbf.toString());
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			return null;
		}
	}

	/**
	 * 查询订单明细
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward orderinfo(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// HttpSession session = request.getSession();
		PrintWriter out = null;
		try {
			out = response.getWriter();
			Integer logid = (Integer) request.getSession()
					.getAttribute("logid");// 网站用户是否已经登录
			Integer shoplogid = (Integer) request.getSession().getAttribute(
					"shoplogid");// 网站用户是否已经登录
			Integer systemlogid = (Integer) request.getSession().getAttribute(
			"systemlogid");// 网站用户是否已经登录
			
			StringBuffer sbf = new StringBuffer(
					"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
			if (logid == null && shoplogid == null&& systemlogid == null) {// 网站用户尚未登录
				// 返回-1
				out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
				return null;
			}

			String orderid = request.getParameter("orderid");

			DdOrder ddorder = this.getDdOrderDAO().findById(
					Integer.parseInt(orderid));
			if (ddorder == null) {// 订单不存在
				out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-2</error></root>");
				return null;
			}
			boolean flag = true;
			if (logid != null
					&& logid.intValue() == ddorder.getUserid().intValue()) {
				flag = false;
			}
			if (shoplogid != null
					&& shoplogid.intValue() == ddorder.getShopid().intValue()) {
				flag = false;
			}
			if (systemlogid != null) {
				flag = false;
			}
			if (flag) {
				out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
				return null;
			}
			
			String date2 = "";
			String code = "";
			if(shoplogid != null||systemlogid!=null){
				code= " code=\""+ddorder.getCode()+"\" ";
			}
			if (ddorder.getType() == 1) {// 订餐、订实物
				DdUserorder du = (DdUserorder) this.getDdUserorderDAO()
						.findByOrderid(ddorder.getId()).get(0);
				DdShop ds = this.getDdShopDAO().findById(du.getShopid());
				DdShopgoods dsg = this.getDdGoodDAO().findById(
						Integer.parseInt(du.getGoodslist()));
				String sizestr = "";
				if(dsg.getStyleType().intValue()==138){
					sizestr = du.getSize()+"寸";
				}
				sbf.append("<ordershop  id=\"" + ddorder.getId()
						+ "\" proname=\"" + dsg.getName()+sizestr + "\" shopname=\""
						+ ds.getName() + "\" goodid=\"" + dsg.getId()
						+ "\" orderid=\"" + (ddorder.getId() + 100000000)
						+ "\" shopaddress=\"" + ds.getAddress()
						+ "\" shopphone=\"" + ds.getPhone() + "\" shopmobile=\""+ds.getMobile()+"\" sname=\""
						+ du.getName() + (du.getSex() == 0 ? "小姐" : "先生")
						+ "\" sphone=\"" + du.getPhone() + "\" dname=\""
						+ du.getName2() + (du.getSex2() == 0 ? "小姐" : "先生")
						+ "\" dphone=\"" + du.getPhone2() + "\" saddress=\""
						+ du.getAddress() + "\" cardtype=\"" + du.getCardtype()
						+ "\" cardvalue=\"" + du.getCardvalue()
						+ "\" signtype=\"" + du.getSigntype()
						+ "\" signvalue=\"" + du.getSignvalue() + "\" pdate=\""
						+ DateStrFormat.getCurrentTime(ddorder.getPaydate())
						+ " \" odate=\""
						+ DateStrFormat.getCurrentTime(du.getOrderdate())
						+ " \" sdate=\"" + du.getSenddate()
						+ "\" cdate=\""+DateStrFormat.getCurrentTime(ddorder.getConfirmdate())+"\"  orderstatus=\"" + ddorder.getStatus()
						+ " \"  price=\"" + ddorder.getPrice()
						+ "\" remark = \"" + du.getRemark() + "\" "+code+"/>");
			} else if (ddorder.getType() == 2) {// 订位置
				DdUserorder1 du = (DdUserorder1) this.getDdUserorder1DAO()
						.findByOrderid(ddorder.getId()).get(0);
				date2 = DateStrFormat.getCurrentTime(du.getOrderdate1());
				DdShop ds = this.getDdShopDAO().findById(du.getShopid());
				String recall = ddorder.getRecall();
//				recall = recall.replaceAll("\n\n", "\n");
//				recall = recall.replaceAll("\n", "\n<br>");
				recall = this.EncodeStr(recall);
				sbf.append("<ordershop  id=\"" + ddorder.getId()
						+ "\" shopname=\"" + ds.getName()
						+ "\"  shopaddress=\"" + ds.getAddress()
						+ "\"  shopid=\"" + ds.getId() + "\"  shopmobile=\""+ds.getMobile()+"\" people=\""
						+ du.getPeople() + "\" loge=\"" + du.getLoge()
						+ "\" orderid=\"" + (ddorder.getId() + 100000000)
						+ "\" price=\"" + ddorder.getPrice()
						+ "\" shopphone=\"" + ds.getPhone() + "\" sname=\""
						+ du.getName() + (du.getSex() == 0 ? "小姐" : "先生")
						+ " \"   sphone=\"" + du.getPhone() + " \"  odate=\""
						+ DateStrFormat.getCurrentTime(du.getOrderdate())
						+ " \"  pdate=\""
						+ DateStrFormat.getCurrentTime(ddorder.getPaydate())
						+ " \" sdate=\"" + date2 + "\" cdate=\""+DateStrFormat.getCurrentTime(ddorder.getConfirmdate())+"\" orderstatus=\""
						+ ddorder.getStatus() + " \" recall=\""+recall+"\" remark = \""
						+ this.EncodeStr(du.getRemark()) + "\" remark1=\""+this.EncodeStr(ddorder.getRemark())+"\" "+code+"  fromto=\""+du.getFromto()+"\"  />");
			}
			sbf.append("</root>");
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			return null;
		}
	}
	/**
	 * 提交订单回访
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward postrecall(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// HttpSession session = request.getSession();
		PrintWriter out = null;
		try {
			out = response.getWriter();
			Integer logid = (Integer) request.getSession()
					.getAttribute("logid");// 网站用户是否已经登录
			Integer shoplogid = (Integer) request.getSession().getAttribute(
					"shoplogid");// 网站用户是否已经登录
			Integer systemlogid = (Integer) request.getSession().getAttribute(
			"systemlogid");// 网站用户是否已经登录
			
			StringBuffer sbf = new StringBuffer(
					"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
			if (logid == null && shoplogid == null&& systemlogid == null) {// 网站用户尚未登录
				// 返回-1
				out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
				return null;
			}

			String orderid = request.getParameter("orderid");

			DdOrder ddorder = this.getDdOrderDAO().findById(
					Integer.parseInt(orderid));
			if (ddorder == null) {// 订单不存在
				out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-2</error></root>");
				return null;
			}
			String recall=request.getParameter("recall");
//			System.out.println("recall:"+recall);
//			if(recall!=null){
//				recall = recall.replaceAll("<br>", "\n");
//			}
			ddorder.setRecall(recall);
			sbf.append("<sign>1</sign></root>");
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			return null;
		}
	}
}
